package com.company.project.web;
import com.alibaba.fastjson.JSON;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.Adviser;
import com.company.project.model.News;
import com.company.project.service.AdviserService;
import com.company.project.service.impl.AdviserServiceImpl;
import com.company.project.util.FileUploadUtil;
import com.company.project.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by CodeGenerator on 2018/10/08.
 */
@RestController
@RequestMapping("/adviser")
public class AdviserController {
    @Resource
    @Autowired
    private AdviserServiceImpl adviserService;

    @Resource
    private FileUploadUtil fileUploadUtil;

    /**
     * 更新顾问
     * @param adviser
     * @return
     */
    @PostMapping("/updateAdviser")
    @ResponseBody
    public Result updateAdviser(@RequestParam String adviser,@RequestParam(required = false) MultipartFile file){
        Adviser adviser1 = JSON.parseObject(adviser,Adviser.class);
        if (file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            adviser1.setPhoneUri(s);
            Boolean aBoolean = adviserService.updateAdviser(adviser1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(aBoolean);
            }
        }
        Boolean aBoolean = adviserService.updateAdviser(adviser1);
        return ResultGenerator.genSuccessResult(aBoolean);
    }

    /**
     * 添加顾问
     * @param adviser
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public Result add(@RequestParam String adviser,@RequestParam(required = false) MultipartFile file) {
        Adviser adviser1 = JSON.parseObject(adviser,Adviser.class);
        if (file!=null){
            String s = fileUploadUtil.writeFile(file, "img");
            if (s == null){
                return ResultGenerator.getInternalServerErrot("图片上传错误");
            }
            adviser1.setPhoneUri(s);
            Boolean aBoolean = adviserService.addAdviser(adviser1);
            if (aBoolean.equals(true)){
                return ResultGenerator.genSuccessResult(aBoolean);
            }
        }
        Boolean aBoolean = adviserService.addAdviser(adviser1);
        return ResultGenerator.genSuccessResult(aBoolean);
    }
    /**
     * 删除专属委员
     * @return
     */
    @GetMapping ("/deleteAR/{id}")
    @ResponseBody
    public Result deleteAR(@PathVariable(value = "id") Integer id){
        Boolean aBoolean = adviserService.deleteAdviser(id);
        return ResultGenerator.genSuccessResult(aBoolean);
    }
    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        adviserService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Adviser adviser) {
        adviserService.update(adviser);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Adviser adviser = adviserService.findById(id);
        return ResultGenerator.genSuccessResult(adviser);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Adviser> list = adviserService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    //获取顾问，分页
    @GetMapping("/list/all/{index}")
    @ResponseBody
    public PageUtil<Adviser> listAll(@PathVariable(value = "index",required = false) Integer index) {
        PageUtil<Adviser> list = adviserService.getAllAdviser(index);
        return list;
    }

    /**
     * 后台查询顾问
     */
    @GetMapping("/selectAll/{index}")
    @ResponseBody
    public Result AllAdviser(@PathVariable(value = "index",required = false) Integer index) {
        PageUtil list = adviserService.selectAdminAdviser(index);
        return ResultGenerator.genSuccessResult(list);
    }

    /**
     * 后台顾问详情
     * @param id
     * @return
     */
    @GetMapping("/AdviserDetails/{id}")
    @ResponseBody
    public Result selectAdviserDetails(@PathVariable(value = "id") Integer id){
        List<Adviser> advisers = adviserService.selectAdviserDetails(id);
        return ResultGenerator.genSuccessResult(advisers);
    }

    /**
     * 前台专家轮播图
     * @return
     */
    @GetMapping("/Picture")
    @ResponseBody
    public Result  selectPicture(){
        List<Adviser> advisers = adviserService.selectPicture();
        return ResultGenerator.genSuccessResult(advisers);
    }

    /**
     * 调整专家顺序
     */
    @PostMapping("/upAdviser")
    @ResponseBody
    public Result AdjustRank(@RequestBody Adviser adviser){
        Boolean aBoolean = adviserService.AdjustRank(adviser);
        return ResultGenerator.genSuccessResult(aBoolean);
    }

    /**
     * 前台查询专家详情
     * @param id
     * @return
     */
    @GetMapping("/particulars/{id}")
    @ResponseBody
    public Result particulars(@PathVariable(value = "id") Integer id){
        List<Adviser> advisers = adviserService.particularsById(id);
        return ResultGenerator.genSuccessResult(advisers);
    }
}
